package com.wangyadong.hobby.schedule.jobtask.dsa;

public class AddBinary {

    public String solution(String a, String b) {
        char[] achars = a.toCharArray();
        char[] bchars = b.toCharArray();
        int carry = 0;

        int alength = achars.length;
        int blength = bchars.length;
        int max = Math.max(alength, blength);
        alength = alength - 1;
        blength = blength - 1;
        java.util.List<Integer> stack = new java.util.ArrayList<>();
        int index = 0;
        for (int i = max - 1; i >= 0; i--) {
            char achar = '0';
            char bchar = '0';

            if (achar != 48 && achar != 49) {
                break;
            }
            if (bchar != 48 && bchar != 49) {
                break;
            }
            int aindex = alength - index;
            if (aindex >= 0) {
                achar = achars[alength - index];
            }
            int bindex = blength - index;
            if (bindex >= 0) {
                bchar = bchars[blength - index];
            }
            //48,49
            int avalue = achar - 48;
            int bvalue = bchar - 48;
            int value = avalue + bvalue + carry;

            if (value > 2) {
                carry = 1;
                value = 1;
            } else if (value > 1) {
                carry = 1;
                value = 0;
            } else {
                carry = 0;
            }
            stack.add(value);

            index++;
        }
        if (carry > 0) {
            stack.add(carry);
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = stack.size() - 1; i >= 0; i--) {
            Integer element = stack.get(i);
            stringBuilder.append(element);
        }

        return stringBuilder.toString();

    }

    public static void main(String[] args) {
        AddBinary binary = new AddBinary();
        String solution = binary.solution("1111001110001001010101001110001101001100101", "1101111100100011010110101110111000000110");
        //        String solution = binary.solution("1101001100101", "000000110");
        //10000111101101101110000000100000100001101011
        //10000111101101101110000000100000100001101011
        System.out.println(solution);








        int a = -11;

        //        System.out.println(a);
        //a的二进制是  00000000000000000000000000001010
        //取反后      11111111111111111111111111110101
        System.out.println(Integer.toBinaryString(a));
        //        System.out.println( (~a));


        //00000000000000000000000000001010
        //

        //
    }
}